package com.ice.day726.sync;

import java.util.ArrayList;
import java.util.Hashtable;

/**
 * @Author ice2020x
 * @Date 2022-08-15 11:42
 * @Description: 把一个局部变量 暴露到外面去，可以重写进行暴露 方法访问修饰符是有意义的
 */
public class Sync3 {
    static final int THREAD_NUMBER = 2;
    static final int LOOP_NUMBER = 200;

    public static void main(String[] args) {
        ThreadSafe3 test = new ThreadSafeSubClass3();
        for (int i = 0; i < THREAD_NUMBER; i++) {
            new Thread(() -> {
                test.method1(LOOP_NUMBER);
            }, "Thread" + (i+1)).start();
        }

        Hashtable table = new Hashtable();
        String str = new String();

    }
}
class ThreadSafe3 {

    public final void method1(int loopNumber) {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < loopNumber; i++) {
            method2(list);
            method3(list);
        }
    }
    public void method2(ArrayList<String> list) {
        list.add("1");

    }
    public void method3(ArrayList<String> list) {
        list.remove(0);
    }
}
class ThreadSafeSubClass3 extends ThreadSafe3{
    @Override
    public void method3(ArrayList<String> list) {
        new Thread(() -> {
            list.remove(0);
        }).start();
    }
}
